#!/bin/bash
# 环境检查脚本

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo_info() {
    echo -e "${GREEN}[✓]${NC} $1"
}

echo_warn() {
    echo -e "${YELLOW}[!]${NC} $1"
}

echo_error() {
    echo -e "${RED}[✗]${NC} $1"
}

echo "======================================"
echo "Hive元数据监控系统 - 环境检查"
echo "======================================"
echo ""

# 1. 检查Python
echo "1. 检查Python环境..."
if command -v python3 &> /dev/null; then
    PYTHON_VERSION=$(python3 --version 2>&1 | awk '{print $2}')
    PYTHON_MAJOR=$(echo $PYTHON_VERSION | cut -d. -f1)
    PYTHON_MINOR=$(echo $PYTHON_VERSION | cut -d. -f2)
    
    if [ "$PYTHON_MAJOR" -ge 3 ] && [ "$PYTHON_MINOR" -ge 8 ]; then
        echo_info "Python版本: $PYTHON_VERSION"
    else
        echo_warn "Python版本较低: $PYTHON_VERSION (建议3.8+)"
    fi
else
    echo_error "未找到Python3"
fi

# 2. 检查pip
echo ""
echo "2. 检查pip..."
if command -v pip3 &> /dev/null; then
    PIP_VERSION=$(pip3 --version | awk '{print $2}')
    echo_info "pip版本: $PIP_VERSION"
else
    echo_error "未找到pip3"
fi

# 3. 检查MySQL客户端
echo ""
echo "3. 检查MySQL客户端..."
if command -v mysql &> /dev/null; then
    MYSQL_VERSION=$(mysql --version | awk '{print $5}' | sed 's/,$//')
    echo_info "MySQL客户端版本: $MYSQL_VERSION"
else
    echo_warn "未找到MySQL客户端（可选）"
fi

# 4. 检查Maven（用于编译Hive Hook）
echo ""
echo "4. 检查Maven（长期方案需要）..."
if command -v mvn &> /dev/null; then
    MVN_VERSION=$(mvn --version | head -1 | awk '{print $3}')
    echo_info "Maven版本: $MVN_VERSION"
else
    echo_warn "未找到Maven（长期方案需要）"
fi

# 5. 检查Java（用于编译Hive Hook）
echo ""
echo "5. 检查Java（长期方案需要）..."
if command -v java &> /dev/null; then
    JAVA_VERSION=$(java -version 2>&1 | head -1 | awk -F '"' '{print $2}')
    echo_info "Java版本: $JAVA_VERSION"
else
    echo_warn "未找到Java（长期方案需要）"
fi

# 6. 检查systemd
echo ""
echo "6. 检查systemd..."
if command -v systemctl &> /dev/null; then
    echo_info "systemd已安装"
else
    echo_warn "未找到systemd"
fi

# 7. 检查网络连通性
echo ""
echo "7. 检查网络..."
if ping -c 1 8.8.8.8 &> /dev/null; then
    echo_info "网络连通"
else
    echo_warn "网络可能不通"
fi

# 8. 检查配置文件
echo ""
echo "8. 检查配置文件..."
if [ -f "config/config.yaml" ]; then
    echo_info "配置文件存在"
    
    # 检查是否配置了Webhook
    if grep -q "webhook_url: \"\"" config/config.yaml || grep -q "webhook_url: ''" config/config.yaml; then
        echo_warn "飞书Webhook URL未配置"
    else
        echo_info "飞书Webhook URL已配置"
    fi
else
    echo_warn "配置文件不存在，将使用示例配置"
fi

# 9. 检查Python依赖
echo ""
echo "9. 检查Python依赖..."
if [ -d "venv" ]; then
    echo_info "虚拟环境已创建"
    
    # 激活虚拟环境并检查依赖
    source venv/bin/activate 2>/dev/null || true
    
    REQUIRED_PACKAGES=("pymysql" "mysql-replication" "pyyaml" "requests")
    for pkg in "${REQUIRED_PACKAGES[@]}"; do
        if python3 -c "import $pkg" 2>/dev/null; then
            echo_info "  - $pkg 已安装"
        else
            echo_warn "  - $pkg 未安装"
        fi
    done
    
    deactivate 2>/dev/null || true
else
    echo_warn "虚拟环境未创建"
fi

# 10. 检查目录权限
echo ""
echo "10. 检查目录权限..."
if [ -w "/var/log" ]; then
    echo_info "/var/log 可写"
else
    echo_warn "/var/log 不可写（需要sudo权限）"
fi

if [ -w "/opt" ]; then
    echo_info "/opt 可写"
else
    echo_warn "/opt 不可写（需要sudo权限）"
fi

echo ""
echo "======================================"
echo "环境检查完成"
echo "======================================"

